import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/max_line_wrap.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:sticky_headers/sticky_headers/widget.dart';

import 'maintain_logic.dart';

class MineMaintainView extends GetView<MineMaintainLogic> {
  const MineMaintainView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: 15,
            ),
            onPressed: Get.back,
          ),
          automaticallyImplyLeading: false,
          backgroundColor:
              ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
          title: Text(
            '我的维修',
            style: StylesUtils.customTextStyle(
                fontSize: 18, fontWeight: FontWeight.w700),
          ),
          centerTitle: true,
        ),
        body: RefreshIndicator(
            onRefresh: () {
              return controller.onRefresh(context);
            },
            child: scrollView()));
  }

  Widget scrollView() {
    return Obx(() => ListView(
          padding: const EdgeInsets.all(10),
          controller: controller.scrollController,
          physics: const AlwaysScrollableScrollPhysics(),
          children: [
            StickyHeader(
              header: Container(
                height: 40,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(5),
                        topRight: Radius.circular(5))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: 80,
                      height: 40,
                      padding: const EdgeInsets.only(left: 15),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '统计',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color1c24,
                            fontWeight: FontWeight.w700),
                      ),
                    ),
                    Container(
                        height: 40,
                        padding: const EdgeInsets.only(right: 15),
                        alignment: Alignment.centerRight,
                        child: SizedBox(
                          height: 25,
                          child: Row(
                              children: controller.tabs
                                  .asMap()
                                  .map((index, value) => MapEntry(
                                      index,
                                      Obx(() => InkWell(
                                          onTap: () {
                                            controller.tabIndex.value = index;
                                          },
                                          child: Container(
                                            constraints: BoxConstraints.tight(
                                                const Size.fromWidth(60)),
                                            alignment: Alignment.center,
                                            decoration: BoxDecoration(
                                                color: index ==
                                                        controller
                                                            .tabIndex.value
                                                    ? ColorsUtils
                                                        .parseColorFromHexString(
                                                            ColorsUtils
                                                                .color0052)
                                                    : Colors.transparent,
                                                border: getBorder(index),
                                                borderRadius:
                                                    getBorderRadius(index)),
                                            child: Text(
                                              value,
                                              style:
                                                  StylesUtils.customTextStyle(
                                                      color: index ==
                                                              controller
                                                                  .tabIndex
                                                                  .value
                                                          ? ColorsUtils
                                                              .colorFfff
                                                          : ColorsUtils
                                                              .color9999,
                                                      fontSize: 13),
                                            ),
                                          )))))
                                  .values
                                  .toList()),
                        ))
                  ],
                ),
              ),
              content: Container(
                  height: 100,
                  margin: const EdgeInsets.only(bottom: 10),
                  padding: const EdgeInsets.symmetric(horizontal: 15),
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5))),
                  child: Row(
                    children: [
                      Expanded(
                          child: Container(
                        padding: const EdgeInsets.only(top: 24),
                        child: Column(
                          children: [
                            Container(
                              height: 26,
                              alignment: Alignment.center,
                              child: Text(
                                '${controller.total.value}',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color0052,
                                    fontWeight: FontWeight.w700,
                                    fontSize: 20),
                              ),
                            ),
                            Container(
                              height: 30,
                              alignment: Alignment.center,
                              child: Text(
                                '维修工单(个)',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color9999),
                              ),
                            )
                          ],
                        ),
                      )),
                      Expanded(
                          child: CustomPaint(
                        painter: CustomPaintVertical(),
                        child: Container(
                          padding: const EdgeInsets.only(top: 24),
                          child: Column(
                            children: [
                              Container(
                                height: 26,
                                alignment: Alignment.center,
                                child: Text(
                                  '${controller.hours.value}',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color0052,
                                      fontWeight: FontWeight.w700,
                                      fontSize: 20),
                                ),
                              ),
                              Container(
                                height: 30,
                                alignment: Alignment.center,
                                child: Text(
                                  '维修时长(h)',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color9999),
                                ),
                              )
                            ],
                          ),
                        ),
                      ))
                    ],
                  )),
            ),
            StickyHeader(
                header: Container(
                  height: 40,
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(5),
                          topRight: Radius.circular(5))),
                  alignment: Alignment.centerLeft,
                  padding: const EdgeInsets.only(left: 15),
                  child: Text(
                    '维修记录',
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color1c24,
                        fontWeight: FontWeight.w700,
                        fontSize: 15),
                  ),
                ),
                content: Container(
                  decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5))),
                  child: Column(
                    children: [
                      ...controller.pageList
                          .map((e) => DrawUnderLine(
                              color: ColorsUtils.parseColorFromHexString(
                                  ColorsUtils.color6666),
                              child: Container(
                                decoration:
                                    const BoxDecoration(color: Colors.white),
                                padding:
                                    const EdgeInsets.symmetric(horizontal: 15),
                                child: ListBody(
                                  children: [
                                    Container(
                                      height: 40,
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        e.title,
                                        style: StylesUtils.customTextStyle(
                                            color: ColorsUtils.color0052,
                                            fontSize: 16,
                                            fontWeight: FontWeight.w700),
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '维修单号',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.orderNo,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '设备名称',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.name,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '设备编号',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.code,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '存放位置',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.location,
                                            overflow: TextOverflow.ellipsis,
                                            maxLines: 1,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      height: 30,
                                      alignment: Alignment.centerLeft,
                                      child: MaxLineWrap(
                                        maxLine: 1,
                                        itemAlignment:
                                            MaxLineWrapAlignment.start,
                                        children: [
                                          Text(
                                            '紧急程度',
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.color3333,
                                                fontSize: 13),
                                          ),
                                          Text(
                                            e.degree.label,
                                            style: StylesUtils.customTextStyle(
                                                color: ColorsUtils.colorAaaa,
                                                fontSize: 13),
                                          )
                                        ],
                                      ),
                                    ),
                                    SizedBox(
                                      height: 40,
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Text(
                                            e.addTime,
                                            style: StylesUtils.customTextStyle(
                                                fontSize: 13,
                                                color: ColorsUtils.color3333),
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              )))
                          .toList(),
                      renderBottom(),
                    ],
                  ),
                ))
          ],
        ));
  }

  BorderRadius? getBorderRadius(int index) {
    if (index == 0) {
      return const BorderRadius.only(
        topLeft: Radius.circular(5),
        bottomLeft: Radius.circular(5),
      );
    }
    if (index == controller.tabs.length - 1) {
      return const BorderRadius.only(
          topRight: Radius.circular(5), bottomRight: Radius.circular(5));
    }
    return null;
  }

  Border getBorder(int index) {
    if (index == 0 || index + 1 == controller.tabs.length) {
      return Border.all(
          color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9));
    }
    if (index == controller.tabs.length - 2) {
      return Border(
        top: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        bottom: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
      );
    }
    return Border(
        top: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        bottom: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)),
        right: BorderSide(
            color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorE9e9)));
  }

  Widget renderBottom() {
    if (controller.isLoading.value) {
      return Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: const Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '努力加载中...',
              style: TextStyle(
                fontSize: 15,
                color: Color(0xFF333333),
              ),
            ),
            Padding(padding: EdgeInsets.only(left: 10)),
            SizedBox(
              width: 20,
              height: 20,
              child: CircularProgressIndicator(strokeWidth: 3),
            ),
          ],
        ),
      );
    }
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 15),
      alignment: Alignment.center,
      child: const Text(
        '上拉加载更多',
        style: TextStyle(
          fontSize: 15,
          color: Color(0xFF333333),
        ),
      ),
    );
  }
}

class CustomPaintVertical extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..isAntiAlias = false
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorE4e4)
      ..style = PaintingStyle.fill;
    Offset p1 = const Offset(0, 20);
    Offset p2 = Offset(0, size.height - 20);
    canvas.drawLine(p1, p2, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
